/**
 * 
 */
package com.tw.trips.service;

import java.util.Calendar;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.neo4j.helpers.collection.ClosableIterable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tw.trips.domain.Alert;
import com.tw.trips.domain.Itinerary;
import com.tw.trips.domain.Trip;
import com.tw.trips.util.HttpUtil;

/**
 * @author M1005264
 * 
 */
@Service
@Transactional
public class TripAlertService {

	private static final String URL = "http://www.trainenquiry.com/RunningIslTrSt.aspx";

	@Autowired
	private TripsService tripsService;

	@Autowired
	private HttpUtil httpSender;

//	@Async
	public void getAlerts() {
		System.out.println("Starting to get Alerts");
		ClosableIterable<Trip> trips = tripsService.getTrips();
		if(trips != null){
			System.out.println("Trips not Null");
			for (Trip trip : trips) {
				Itinerary itnry = trip.getItinerary();
				if (itnry != null && isAlertRequired(itnry)) {
					System.out.println("Itnry not Null");		
					Alert alert = getAlertForItineary(itnry);
					trip = tripsService.addAlertToTrip(alert, trip);
				}
			}
		}
		System.out.println("Finished to get Alerts");
	}

	public Alert getAlertForItineary(Itinerary itnry) {
		String param = "tr=12676&st=KPD+&dt=12%2f01%2f2012";

		String trainNum = itnry.getCarrierNum();
		String stationCode = itnry.getFromCode();
		Date travelDate = itnry.getTravelDate();
		Calendar cal = Calendar.getInstance();
		cal.setTime(travelDate);

		int day = cal.get(Calendar.DAY_OF_MONTH);
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);

		param = "tr="+trainNum+"&st="+stationCode+"&dt="+getFormatted(day)+"%2f"+getFormatted(month)+"%2f"+getFormatted(year);

		String response = httpSender.sendGetRequest(URL, param);

//		String formatted = response.toUpperCase();
/*		formatted = formatted.replace("<B>", "");
		formatted = formatted.replace("</B>", "");*/
		Document doc = Jsoup.parse(response);
		Element data = doc.getElementById("Table3");
		System.out.println("Result --- " + data.toString());
		
		return buildAlertForTrip(data.toString());
	}

	private Alert buildAlertForTrip(String htmlString) {
		Alert alert = new Alert();
		alert.setHtml(htmlString);

		return alert;
	}

	private String getFormatted(int day) {
		if(day < 10){
			return "0"+day;
		}
		return ""+day;
	}

	private boolean isAlertRequired(Itinerary itnry) {
		Date travelDate = itnry.getTravelDate();
		int dateDiff = travelDate.compareTo(Calendar.getInstance().getTime());
		if (dateDiff == -1 || dateDiff == 0) {
			return true;
		}
		return false;
	}
	
	public Alert getMockAlert(){
		String mockHtml = "<table class=\"txt\" id=\"Table3\" cellSpacing=\"1\" cellPadding=\"5\"\r\n" + 
				"	width=\"400\" align=\"center\" bgColor=\"#ffff4e\" border=\"0\">\r\n" + 
				"	<TBODY>\r\n" + 
				"		<TR>\r\n" + 
				"			<td class=\"alternat_table_row\">\r\n" + 
				"				<B>Train Name</B>\r\n" + 
				"			</td>\r\n" + 
				"			<TD class=\"alternat_table_row\">KOVAI EXP</TD>\r\n" + 
				"		</TR>\r\n" + 
				"		<tr>\r\n" + 
				"			<td class=\"alternat_table_row\">\r\n" + 
				"				<B>Station Name</B>\r\n" + 
				"			</td>\r\n" + 
				"			<TD class=\"alternat_table_row\">KATPADI JN</TD>\r\n" + 
				"		</tr>\r\n" + 
				"		<TR>\r\n" + 
				"			<td class=\"td_background\" bgColor=\"#fff4ea\">Scheduled\r\n" + 
				"				Arrival Time\r\n" + 
				"			</td>\r\n" + 
				"			<TD class=\"td_background\" bgColor=\"#fff4ea\">19:33, 12\r\n" + 
				"				Jan 2012\r\n" + 
				"			</TD>\r\n" + 
				"		</TR>\r\n" + 
				"		<TR>\r\n" + 
				"			<td class=\"td_background\" bgColor=\"#fff4ea\">Delay In\r\n" + 
				"				Arrival (Hr:Min)\r\n" + 
				"			</td>\r\n" + 
				"			<TD class=\"td_background\" bgColor=\"#fff4ea\">No Delay</TD>\r\n" + 
				"		</TR>\r\n" + 
				"		<TR>\r\n" + 
				"			<td class=\"td_background\" bgColor=\"#fff4ea\">Actual/Expected\r\n" + 
				"				Arrival\r\n" + 
				"			</td>\r\n" + 
				"			<TD class=\"td_background\" bgColor=\"#fff4ea\">19:33, 12\r\n" + 
				"				Jan 2012\r\n" + 
				"			</TD>\r\n" + 
				"		</TR>\r\n" + 
				"		<TR>\r\n" + 
				"			<td class=\"txt\">Scheduled Departure Time</td>\r\n" + 
				"			<TD class=\"txt\">19:35, 12 Jan 2012</TD>\r\n" + 
				"		</TR>\r\n" + 
				"		<TR>\r\n" + 
				"			<td class=\"txt\">Delay In Departure (Hr:Min)</td>\r\n" + 
				"			<TD class=\"txt\">No Delay</TD>\r\n" + 
				"		</TR>\r\n" + 
				"		<TR>\r\n" + 
				"			<td class=\"txt\">Actual/Expected Departure</td>\r\n" + 
				"			<TD class=\"txt\">19:35, 12 Jan 2012</TD>\r\n" + 
				"		</TR>\r\n" + 
				"	</TBODY>\r\n" + 
				"</table>";
		Alert alert = new Alert();
		alert.setHtml(mockHtml);
		return alert;
	}
}
